﻿Shader "RenderingPractice/Diffuse Vertex-Level"//逐顶点光照（漫反射）
{
    Properties
    {
        _Diffuse ("Diffuse", Color) = (1,1,1,1)//漫反射颜色
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }

        Pass
        {
            CGPROGRAM
            #pragma vertex VertexFunction
            #pragma fragment FragmentFunction

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
			
            fixed4 _Diffuse;//漫反射颜色

            struct VertexIn
            {
                float4 vertex : POSITION; 
				float3 normal :NORMAL;//模型空间表面法线
            };

            struct VertexOut
            {
                float4 vertex : SV_POSITION;
				fixed3 vertexColor:COLOR;
            };
			//顶点函数
            VertexOut VertexFunction (VertexIn vi)
            {
                VertexOut vo;
				//1.环境光：
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				//2.漫反射：
				fixed3 worldNormalDir = UnityObjectToWorldNormal(vi.normal);//n
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);//l
				fixed3 diffuseColor = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormalDir ,worldLightDir));
				//3.光照颜色：
				vo.vertexColor = ambient + diffuseColor;
				//顶点坐标：模型空间=>裁剪空间
                vo.vertex = UnityObjectToClipPos(vi.vertex);
                return vo;
            }
			//片元函数
            fixed4 FragmentFunction (VertexOut fi) : SV_Target
            {
				fixed4 c = fixed4(fi.vertexColor ,1.0);
                return c;
            }
            ENDCG
        }
    }
	FallBack "Diffuse"
}
